package com.xoa.service.party.impl;

import com.xoa.dao.workflow.FlowProcessMapper;
import com.xoa.dao.workflow.FlowRunMapper;
import com.xoa.dao.workflow.FlowRunPrcsMapper;
import com.xoa.dao.workflow.FlowTypeModelMapper;
import com.xoa.model.workflow.FlowProcess;
import com.xoa.model.workflow.FlowRun;
import com.xoa.model.workflow.FlowRunPrcs;
import com.xoa.model.workflow.FlowRunPrcsExcted;
import com.xoa.service.party.StreetFlowRunPrcsService;
import com.xoa.service.users.UsersService;
import com.xoa.util.AjaxJson;
import com.xoa.util.DateFormat;
import com.xoa.util.ToJson;
import com.xoa.util.common.L;
import com.xoa.util.common.StringUtils;
import com.xoa.util.page.PageParams;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class StreetFlowRunPrcsServiceImpl implements StreetFlowRunPrcsService {

	@Resource
	private FlowRunPrcsMapper flowRunPrcsMapper;

	@Resource
	private UsersService usersService;

	@Resource
	private FlowTypeModelMapper flowTypeModelMapper;

	@Resource
	private FlowProcessMapper flowProcessMapper;

	@Resource
	private FlowRunMapper flowRunMapper;


	@Override
	public void save(FlowRunPrcs flowRunPrcs) {
		flowRunPrcsMapper.insertSelective(flowRunPrcs);
	}

	/**
	 * 创建作者:   张勇
	 * 创建日期:   2017/5/24 20:29
	 * 方法介绍:   查询待办工作
	 * 参数说明:  修整时间计算
	 *
	 * @return
	 */
	@SuppressWarnings("all")
	@Override
	public ToJson<FlowRunPrcs> selectObject(Map<String, Object> maps, Integer page,
                                            Integer pageSize, boolean useFlag) {
		ToJson<FlowRunPrcs> toJson = new ToJson<FlowRunPrcs>();
		PageParams pageParams = new PageParams();
		pageParams.setPage(page);
		pageParams.setPageSize(pageSize);
		pageParams.setUseFlag(useFlag);
		maps.put("page", pageParams);
		List<FlowRunPrcs> list = flowRunPrcsMapper.selectObjcet(maps);
		List<FlowRunPrcs> returnList = new ArrayList<FlowRunPrcs>();
		int len = list.size();
		if (len > 0) {
			toJson.setTotleNum(pageParams.getTotal());
			Date newDate = new Date();
			for (FlowRunPrcs flowRunPrcs : list) {
				flowRunPrcs.setUserName(usersService.getUserNameById(flowRunPrcs.getUserId()));
				flowRunPrcs.setFlowType(flowTypeModelMapper.queryOneObject(flowRunPrcs.getFlowRun().getFlowId()));
				//查询流程并设置流程
				FlowProcess flowProcess = flowProcessMapper.findProcess(flowRunPrcs.getFlowRun().getFlowId(), flowRunPrcs.getFlowPrcs());
				if (flowProcess != null) {
					flowRunPrcs.setFlowProcess(flowProcess);
				}

				if (DateFormat.getTime(flowRunPrcs.getPrcsTime()) != -621701856) {
//				if(!StringUtils.checkNull(flowRunPrcs.getPrcsTime())) {
					// 工作接收时间
					flowRunPrcs.setReceive(DateFormat.getStrTime(DateFormat.getTime(flowRunPrcs.getPrcsTime())));
					// 办理停留时间
					flowRunPrcs.setHandleTime(DateFormat.returnTime((DateFormat.getTime(DateFormat.getStrDate(newDate))) - DateFormat.getTime(flowRunPrcs.getPrcsTime())));
				}
				//计算时间转换
				if (DateFormat.getTime(flowRunPrcs.getPrcsTime()) != -621701856 && flowRunPrcs.getPrcsId() > 1 ||
						DateFormat.getTime(flowRunPrcs.getPrcsTime()) == -621701856 && flowRunPrcs.getPrcsId() > 1) {
					Integer runId = flowRunPrcs.getRunId();
					Integer prcsId = flowRunPrcs.getPrcsId() - 1;
					String userId = (String) maps.get("userId");
					maps.put("prcsId", prcsId);
					maps.put("runId", runId);
					String upTime = flowRunPrcsMapper.findTimeBy(maps);
					if (!StringUtils.checkNull(upTime)) {
						// 到达时间
						flowRunPrcs.setReceiptTime(DateFormat.getStrTime(DateFormat.getTime(upTime)));
						// 到达停留时间
						flowRunPrcs.setArriveTime(DateFormat.returnTime((DateFormat.getTime(DateFormat.getStrDate(newDate))) - DateFormat.getTime(flowRunPrcs.getReceiptTime())));
					}
				} else {
					flowRunPrcs.setReceiptTime(DateFormat.getStrTime(DateFormat.getTime(flowRunPrcs.getPrcsTime())));
					flowRunPrcs.setArriveTime(DateFormat.returnTime((DateFormat.getTime(DateFormat.getStrDate(newDate))) - DateFormat.getTime(flowRunPrcs.getPrcsTime())));
					flowRunPrcs.setReceive("");
					flowRunPrcs.setHandleTime("");
				}
				FlowRun flowRun = flowRunMapper.find(flowRunPrcs.getRunId());
				String userName = usersService.getUserNameById(flowRun.getBeginUser());
				flowRunPrcs.setUserName(userName);
				returnList.add(flowRunPrcs);
			}
			toJson.setFlag(0);
			toJson.setMsg("ok");
			toJson.setObj(returnList);
		} else {
			toJson.setFlag(1);
			toJson.setMsg("error");
		}
		return toJson;
	}

	/**
	 * 创建作者:   张勇
	 * 创建日期:   2017/5/24 20:30
	 * 方法介绍:   查询办结工作
	 * 参数说明:
	 *
	 * @return
	 */
	@Override
	public ToJson<FlowRunPrcs> selectEnd(Map<String, Object> maps, Integer page,
                                         Integer pageSize, boolean useFlag) {
		ToJson<FlowRunPrcs> toJson = new ToJson<FlowRunPrcs>(1, "error");
		PageParams pageParams = new PageParams();
		pageParams.setPage(page);
		pageParams.setPageSize(pageSize);
		pageParams.setUseFlag(useFlag);
		maps.put("page", pageParams);
		List<FlowRunPrcs> list = flowRunPrcsMapper.selectEnd(maps);
		List<FlowRunPrcs> returnList = new ArrayList<FlowRunPrcs>();
		int len = list.size();
		if (len > 0) {
			toJson.setTotleNum(pageParams.getTotal());
			for (FlowRunPrcs flowRunPrcs : list) {
				flowRunPrcs.setUserName(usersService.getUserNameById(flowRunPrcs.getUserId()));
				flowRunPrcs.setFlowType(flowTypeModelMapper.queryOneObject(flowRunPrcs.getFlowRun().getFlowId()));
				//查询流程并设置流程
				FlowProcess flowProcess = flowProcessMapper.findProcess(flowRunPrcs.getFlowRun().getFlowId(), flowRunPrcs.getFlowPrcs());
				if (flowProcess != null) {
					flowRunPrcs.setFlowProcess(flowProcess);
				}
				if (!StringUtils.checkNull(flowRunPrcs.getDeliverTime())) {
					flowRunPrcs.setDeliverTime(DateFormat.getStrTime(DateFormat.getTime(flowRunPrcs.getDeliverTime())));
				}
				returnList.add(flowRunPrcs);
			}
			toJson.setFlag(0);
			toJson.setMsg("ok");
			toJson.setObj(returnList);
		} else {
			toJson.setFlag(1);
			toJson.setMsg("error");
		}
		return toJson;
	}

	/**
	 * 创建作者:   张勇
	 * 创建日期:   2017/5/25 15:47
	 * 方法介绍:   查询挂起工作
	 * 参数说明:
	 *
	 * @return
	 */
	@SuppressWarnings("all")
	@Override
	public ToJson<FlowRunPrcs> selectHang(Map<String, Object> maps, Integer page,
                                          Integer pageSize, boolean useFlag) {
		ToJson<FlowRunPrcs> toJson = new ToJson<FlowRunPrcs>();
		PageParams pageParams = new PageParams();
		pageParams.setPage(page);
		pageParams.setPageSize(pageSize);
		pageParams.setUseFlag(useFlag);
		maps.put("page", pageParams);
		List<FlowRunPrcs> list = flowRunPrcsMapper.selectHang(maps);
		List<FlowRunPrcs> returnList = new ArrayList<FlowRunPrcs>();
		int len = list.size();
		if (len > 0) {
			toJson.setTotleNum(pageParams.getTotal());
			for (FlowRunPrcs flowRunPrcs : list) {
				flowRunPrcs.setUserName(usersService.getUserNameById(flowRunPrcs.getUserId()));
				flowRunPrcs.setFlowType(flowTypeModelMapper.queryOneObject(flowRunPrcs.getFlowRun().getFlowId()));
				returnList.add(flowRunPrcs);
			}
			toJson.setFlag(0);
			toJson.setMsg("ok");
			toJson.setObj(returnList);
		} else {
			toJson.setFlag(1);
			toJson.setMsg("error");
		}
		return toJson;
	}

	/**
	 * 创建作者:   张勇
	 * 创建日期:   2017/5/25 15:47
	 * 方法介绍:   查询所有工作
	 * 参数说明:
	 *
	 * @return
	 */
	@SuppressWarnings("all")
	@Override
	public ToJson<FlowRunPrcs> selectAll(Map<String, Object> maps, Integer page, Integer pageSize, boolean useFlag) {
		ToJson<FlowRunPrcs> toJson = new ToJson<FlowRunPrcs>(1, "error");
		PageParams pages = new PageParams();
		pages.setPage(page);
		pages.setPageSize(pageSize);
		pages.setUseFlag(useFlag);
		maps.put("page", pages);
		List<FlowRunPrcs> list = flowRunPrcsMapper.selectAll(maps);
		List<FlowRunPrcs> returnList = new ArrayList<FlowRunPrcs>();
		int len = list.size();
		if (len > 0) {
			toJson.setTotleNum(pages.getTotal());
			for (FlowRunPrcs flowRunPrcs : list) {
				flowRunPrcs.setUserName(usersService.getUserNameById(flowRunPrcs.getUserId()));
				flowRunPrcs.setFlowType(flowTypeModelMapper.queryOneObject(flowRunPrcs.getFlowRun().getFlowId()));
				FlowProcess flowProcess = flowProcessMapper.findProcess(flowRunPrcs.getFlowRun().getFlowId(), flowRunPrcs.getFlowPrcs());
				if (flowProcess != null) {
					flowRunPrcs.setFlowProcess(flowProcess);
				}
				returnList.add(flowRunPrcs);
			}
			toJson.setFlag(0);
			toJson.setMsg("ok");
			toJson.setObj(returnList);
		} else {
			toJson.setFlag(1);
			toJson.setMsg("error");
		}
		return toJson;
	}

	/**
	 * 创建作者:   张勇
	 * 创建日期:   2017/6/20 13:52
	 * 方法介绍:   删除我的工作待办第一步
	 * 参数说明:
	 *
	 * @return
	 */
	@Override
	@Transactional
	public ToJson<FlowRunPrcs> deleteRunPrcs(FlowRunPrcs flowRunPrcs) {
		ToJson<FlowRunPrcs> toJson = new ToJson<FlowRunPrcs>(1, "error");
		try {
			Integer flag = flowRunPrcsMapper.selectPrcs(flowRunPrcs.getId());
			if (flag == 1) {
				flowRunPrcsMapper.deleteWork(flowRunPrcs);
				toJson.setFlag(0);
				toJson.setMsg("ok");
			} else {
				//重名
				toJson.setMsg("repetition");
			}
		} catch (Exception e) {
			L.e("FlowRunPrcsService deleteRunPrcs:" + e);
		}
		return toJson;
	}

	public List<FlowRunPrcs> findByRunId(Map<String, Object> maps) {
		List<FlowRunPrcs> l = flowRunPrcsMapper.selectfrp(maps);
		return l;

	}

	@Override
	@Transactional
	public void update(FlowRunPrcsExcted flowRunPrcs) {
		flowRunPrcsMapper.updateByPrimaryKeySelective(flowRunPrcs);
	}

	//	public  String returnNode (Integer runId){
//		String returnNodes = "";
//		try {
//			List<FlowRunPrcs> list = flowRunPrcsMapper.findAllNode(runId);
//			int leng = list.size();
//			for (int i = 0; i < leng; i++) {
//				String flag = list.get(i).getPrcsFlag();
//				if (!"3".equals(flag) || !"4".equals(flag)) {
//					list1.add(list.get(i));
//					break;
//				}
//			}
//		}catch (Exception e){
//			L.e("ReturnNode Error:"+e);
//		}
//		return toJson;
//	}
	@Transactional
	@Override
	public int updateSql(Map<String, Object> map) {
		int a = flowRunPrcsMapper.updateSome(map);
		return a;
	}

	@Override
	public List<FlowRunPrcs> findlessRunId(Map<String, Object> maps) {
		List<FlowRunPrcs> list = flowRunPrcsMapper.findlessRunId(maps);
		return list;
	}

	@Override
	public AjaxJson entrustWork(Map<String, Object> maps, Integer page,
                                Integer pageSize, boolean useFlag) {
		AjaxJson ajaxJson = new AjaxJson();
		PageParams pages = new PageParams();
		pages.setPage(page);
		pages.setPageSize(pageSize);
		pages.setUseFlag(useFlag);
		maps.put("page", pages);
		List<FlowRunPrcs> list = flowRunPrcsMapper.entrustWork(maps);
		int len = list.size();
		if (len > 0) {
			ajaxJson.setTotleNum(pages.getTotal());
			for (FlowRunPrcs flowRunPrcs : list) {
				flowRunPrcs.setUserName(usersService.getUserNameById(flowRunPrcs.getUserId()));
				flowRunPrcs.setFlowType(flowTypeModelMapper.queryOneObject(flowRunPrcs.getFlowRun().getFlowId()));
				FlowProcess flowProcess = flowProcessMapper.findProcess(flowRunPrcs.getFlowRun().getFlowId(), flowRunPrcs.getFlowPrcs());
				if (flowProcess != null) {
					flowRunPrcs.setFlowProcess(flowProcess);
				}
			}
		}

		return null;

	}



	/**
	 * 创建作者:   zlf
	 * 创建日期:   2017/11/9 17:04
	 * 方法介绍:   查询是否强制转交
	 * 参数说明:
	 * @return
	 */
	public Integer checkGatherNode(Map<String, Object> maps){
		return flowRunPrcsMapper.checkGatherNode(maps);
	}
}